package com.cmf.myproject.controller.system.bossuser;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmf.myproject.dal.mybatis.model.BossUser;
import com.cmf.myproject.dal.mybatis.mymodel.BossUserRole;
import com.cmf.myproject.service.system.sysrole.SysRoleService;
import com.cmf.myproject.service.system.user.BossUserService;
import com.cmf.myproject.service.system.user.dto.BossUserAddItemReqDto;
import com.cmf.myproject.service.system.user.dto.BossUserQueryFilter;
import com.cmf.myproject.service.system.user.dto.BossUserUpdateItemReqDto;
import com.system.core.controller.ControllerBase;
import com.system.core.results.ResultBase;
import com.system.core.view.layui.table.TableViewRspDto;

@Controller
@RequestMapping("boss/boss_user")
public class BossUserController extends ControllerBase {
	@Autowired
	private BossUserService bossUserService;
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@RequiresPermissions("user_topage")
	@RequestMapping(value = "to_page",method = RequestMethod.GET)
	public String toPage() {
		getModelMap().addAttribute("roles", sysRoleService.sysRoleListAll());
		return "/system/bossUser/bossUser-list";
	}
	
	@RequestMapping(value = "query_list",method = RequestMethod.POST)
	@ResponseBody
	public TableViewRspDto<BossUserRole> bossUserQueryList(BossUserQueryFilter filter) {
		TableViewRspDto<BossUserRole> rsp = bossUserService.bossUserList(filter);
		return rsp;
	}
	
	@RequestMapping(value = "get_by_pk",method = RequestMethod.POST)
	@ResponseBody
	public BossUser bossUserGetByPrimaryKey(BossUser bossUser){
		return bossUserService.bossUserGetByPrimaryKey(bossUser);
	}
	
	@RequiresPermissions("user_add_opt")
	@RequestMapping(value = "add_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase bossUserAddItem(@Validated BossUserAddItemReqDto bossUserAddItemReqDto) {
		return bossUserService.bossUserSave(bossUserAddItemReqDto);
	}
	
	@RequestMapping(value = "update_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase bossUserUpdateItem(@Validated BossUserUpdateItemReqDto bossUserUpdateItemReqDto) {
		return bossUserService.bossUserUpdateByPrimaryKey(bossUserUpdateItemReqDto);
	}
	
	@RequiresPermissions("user_delete_opt")
	@RequestMapping(value = "delete_item",method = RequestMethod.POST)
	@ResponseBody
	public ResultBase bossUserDeleteItem(@RequestBody List<Long> keys) throws Exception{
		
		for(Long id:keys) {
			if(id == 1) {
				throw new Exception("不能删除超级管理员");
			}
		}
		return bossUserService.bossUserDeleteByPrimaryKey(keys);
	}
}
